package com.example.feidaolumanager.pages

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.feidaolumanager.R
import com.example.feidaolumanager.pages.ui.theme.FeidaoluManagerTheme
import com.example.feidaolumanager.viewModel.SettingViewModel
import com.tencent.upgrade.core.UpgradeManager
import com.tencent.upgrade.core.UpgradeReqCallbackForUserManualCheck


class SettingActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        setContent {
            FeidaoluManagerTheme {
                // A surface container using the 'background' color from the theme
                Page()
            }
        }
    }

    @Composable
    private fun Page(settingViewModel: SettingViewModel = viewModel(factory = SettingViewModel.Factory)) {
        Surface(
            modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background
        ) {
            Column(
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Text(text = "是否弹出导航弹窗: ")
                    Switch(checked = settingViewModel.isShowNavigationDialog.collectAsState().value,
                        onCheckedChange = {
                            Log.e(
                                this.toString(),
                                "Page: 更新前 ${settingViewModel.isShowNavigationDialog.value}"
                            )
                            settingViewModel.selectIfShowNavigationDialog(!settingViewModel.isShowNavigationDialog.value)
                        })
                }

                val versionCode by remember {
                    val versionCode = applicationContext.packageManager.getPackageInfo(
                        applicationContext
                            .packageName, 0
                    ).versionName
                    mutableStateOf(versionCode)
                }

                Text(
                    text = "当前版本:${versionCode}"
                )

                Button(onClick = {
                    UpgradeManager.getInstance()
                        .checkUpgrade(true, null, object : UpgradeReqCallbackForUserManualCheck() {
                            override fun onReceivedNoStrategy() {
                                super.onReceivedNoStrategy()
                                Toast.makeText(
                                    applicationContext,
                                    "没有可用的更新",
                                    Toast.LENGTH_SHORT
                                ).show()
                            }
                        })
                }, shape = MaterialTheme.shapes.small) {
                    Text(text = "检查更新")
                }

                Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
                    Image(
                        painter = painterResource(id = R.drawable.feidaolu),
                        contentDescription = "app下载二维码",
                        modifier = Modifier.size(300.dp)
                    )
                }
            }

        }
    }
}